package com.elong.businesstravel.base.baseactivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.dialog.CustomWaitDialog;
import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.universalimageloader.core.DisplayImageOptions;
import com.android.support.jhf.utils.DateUtils;
import com.baidu.location.BDLocation;
import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baidusdk.map.InitBaiduMap;
import com.elong.businesstravel.base.mcrypt.MCrypt;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.entitys.City;
import com.elong.businesstravel.entitys.HotelSearchCondition;
import com.elong.businesstravel.entitys.TrainStation;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;

/**
 * activity基类
 * 
 * @author lenovo
 * 
 */
abstract public class BaseFragmentActivity extends FragmentActivity {

	protected Context mContext;
	
	protected Bundle mSavedInstanceState;

	protected ELongBusinessTravelApplication mApplication;

	private CustomWaitDialog mCustomWaitDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mSavedInstanceState = savedInstanceState;
		
		getWindow().setFormat(PixelFormat.RGBA_8888);

		mContext = this;

		mApplication = (ELongBusinessTravelApplication) mContext
				.getApplicationContext();
		// 友盟推送
		PushAgent.getInstance(mContext).onAppStart();

		DebugLog.setTag(mContext.getClass().getName());
		
		if(null != savedInstanceState){
			mApplication.mCity = (City) savedInstanceState.getSerializable("city");
			mApplication.mLocationCity = (City)savedInstanceState.getSerializable("locationcity");
			mApplication.mHotelSearchCondition = (HotelSearchCondition) savedInstanceState.getSerializable("hotelsearchcondition");
			mApplication.mFlagHome = savedInstanceState.getBoolean("flaghome");
			mApplication.mWeChatCode = savedInstanceState.getString("wecharcode");
			mApplication.mTrainStations = (ArrayList<TrainStation>) savedInstanceState.getSerializable("trainstations");
		}
	}

	protected void init() {

	}

	protected void setListener() {

	}

	protected void setTitleNavBar() {

	}

	/**
	 * 显示等待对话框
	 * 
	 * @param messageString
	 * @param onDismissListener
	 */
	public void showWaitDialog(String messageString,
			final OnKeyCancel onKeyCancel) {
		if (null == mCustomWaitDialog) {
			mCustomWaitDialog = new CustomWaitDialog(mContext,
					com.android.support.jhf.R.style.CustomWaitDialog);
			mCustomWaitDialog.setCancelable(true);
			mCustomWaitDialog.setCanceledOnTouchOutside(false);
			mCustomWaitDialog.setMessage(messageString);
			mCustomWaitDialog.setOnKeyCancelListener(new OnKeyCancel() {

				@Override
				public void onKeyCancelListener() {
					if (null != onKeyCancel) {
						onKeyCancel.onKeyCancelListener();
					}
					if (null != mCustomWaitDialog) {
						mCustomWaitDialog.cancel();
						mCustomWaitDialog = null;
					}
				}
			});
			mCustomWaitDialog.show();
		}
	}

	private final BroadcastReceiver mTimeTickBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_TIME_TICK)) {
//				HandlerToastUI.getHandlerToastUI(mContext,
//						"mTimeTickBroadcastReceiver" + mContext.getClass());

				String currentDateString = DateUtils.getCurrentDate("yyyyMMdd");
				String startDateString = DateUtils.dateFormat(
						mApplication.mHotelDestineDateStrings[0],
						"yyyy-MM-dd (E)", "yyyyMMdd");

				Date currentDate = new Date();
				currentDate.setTime(DateUtils.getDateMillis(currentDateString,
						"yyyyMMdd"));

				Date startDate = new Date();
				startDate.setTime(DateUtils.getDateMillis(startDateString,
						"yyyyMMdd"));
				
				if (currentDate.compareTo(startDate) != 0) {
//					HandlerToastUI.getHandlerToastUI(mContext, "currentDate != startDate");
					mApplication.factoryDate();
					refreshDate();
				}
			}
		}
	};
	
	/**
	 * 每次都需要刷新搜索条件的日期
	 */
	private void refreshDate() {
		// 从缓存中取
		List<Date> mSaveDates = SaveUserOperate.getInOutHotelDates(mContext);
		checkInOutHotelDates(mSaveDates);
		// 设置默认
		mApplication.mHotelSearchCondition.mSearchConditionDate = mApplication.mHotelDestineDateStrings[0];
		mApplication.mHotelSearchCondition.mSearchConditionDays = "1晚";
		// 缓存有效设置缓存
		if (null != mSaveDates && 0 != mSaveDates.size()) {
			mApplication.mHotelSearchCondition.mSearchConditionDate = DateUtils
					.dateFormat(mSaveDates.get(0).getTime(), "yyyy-MM-dd (E)");
			mApplication.mHotelSearchCondition.mSearchConditionDays = (mSaveDates
					.size() - 1) + "晚";
		}
		// 更新显示
		String dateString = DateUtils.dateFormat(DateUtils.getDateMillis(
				mApplication.mHotelSearchCondition.mSearchConditionDate,
				"yyyy-MM-dd (E)"), "MM/dd");
//		mDateTextView.setText(dateString + " 住"
//				+ mApplication.mHotelSearchCondition.mSearchConditionDays);
		refreshDateUI(dateString);
	}
	
	/**
	 * 核对从缓存中获取的时间
	 */
	private void checkInOutHotelDates(List<Date> mSaveDates) {
		if (null != mSaveDates && mSaveDates.size() > 0) {
			Date startSaveDate = mSaveDates.get(0);
			String startSaveDateString = DateUtils.dateFormat(startSaveDate.getTime(),
					"yyyyMMdd");
			startSaveDate.setTime(DateUtils.getDateMillis(startSaveDateString,
					"yyyyMMdd"));
			
			Date endSaveDate = mSaveDates.get(mSaveDates.size()-1);
			String endSaveDateString = DateUtils.dateFormat(endSaveDate.getTime(),
					"yyyyMMdd");
			endSaveDate.setTime(DateUtils.getDateMillis(endSaveDateString,
					"yyyyMMdd"));

			String startHotel = mApplication.mHotelDestineDateStrings[0];
			Date startHotelDate = new Date(DateUtils.getDateMillis(startHotel, "yyyy-MM-dd (E)"));
			
			String endHotel = mApplication.mHotelDestineDateStrings[mApplication.mHotelDestineDateStrings.length-1];
			Date endHotelDate = new Date(DateUtils.getDateMillis(endHotel, "yyyy-MM-dd (E)"));
			
			if(startHotelDate.compareTo(startSaveDate) > 0 || endHotelDate.compareTo(endSaveDate) < 0){
				mSaveDates.clear();
				SaveUserOperate.setInOutHotelDates(mContext,
						new ArrayList<Date>());
			}else {
				
			}

		}
	}
	
	/**
	 * 刷新时间UI
	 */
	protected void refreshDateUI(String showDateUIString) {
		
	}

	public void hideWaitDialog() {
		if (null != mCustomWaitDialog) {
			mCustomWaitDialog.cancel();
			mCustomWaitDialog = null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);

//		// 时间改变广播
//		IntentFilter filter = new IntentFilter();
//		filter.addAction(Intent.ACTION_TIME_TICK);
//		registerReceiver(mTimeTickBroadcastReceiver, filter);
		
//		HandlerToastUI.getHandlerToastUI(mContext, mContext.getClass().toString());
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);

//		unregisterReceiver(mTimeTickBroadcastReceiver);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putSerializable("city", mApplication.mCity);
		outState.putSerializable("locationcity", mApplication.mLocationCity);
		outState.putSerializable("hotelsearchcondition", mApplication.mHotelSearchCondition);
		outState.putBoolean("flaghome", mApplication.mFlagHome);
		outState.putString("wecharcode", mApplication.mWeChatCode);
		outState.putSerializable("trainstations", (ArrayList<TrainStation>)mApplication.mTrainStations);
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if(null != savedInstanceState){
			mApplication.mCity = (City) savedInstanceState.getSerializable("city");
			mApplication.mLocationCity = (City)savedInstanceState.getSerializable("locationcity");
			mApplication.mHotelSearchCondition = (HotelSearchCondition) savedInstanceState.getSerializable("hotelsearchcondition");
			mApplication.mFlagHome = savedInstanceState.getBoolean("flaghome");
			mApplication.mWeChatCode = savedInstanceState.getString("wecharcode");
			mApplication.mTrainStations = (ArrayList<TrainStation>) savedInstanceState.getSerializable("trainstations");
		}
	}

}
